#ifndef MODELEDIT_H
#define MODELEDIT_H

#include <QDialog>
#include <QtGui>
#include <QPen>
#include <QtXml>
#include "mixerslist.h"
#include "myeeprom.h"


#define PIPO_EEPROM_FILE_TYPE          "PIPO_EEPROM_FILE"
#define PIPO32_EEPROM_FILE_TYPE        "PIPO32_EEPROM_FILE"

#define HEX_FILES_FILTER     "HEX files (*.hex);;"
#define BIN_FILES_FILTER     "BIN files (*.bin);;"
#define PIPO_FILES_FILTER    "PIPO EEPROM files (*.pipo);;"
#define PIPO_ALL_FILES_FILTER    "All PIPO files (*.pipo *.bin *.hex);;"
#define FLASH_FILES_FILTER   "FLASH files (*.bin *.hex);;" BIN_FILES_FILTER HEX_FILES_FILTER
#define EXTERNAL_EEPROM_FILES_FILTER   "EEPROM files (*.bin *.hex);;" BIN_FILES_FILTER HEX_FILES_FILTER
#define EEPROM_FILES_FILTER  PIPO_FILES_FILTER PIPO_ALL_FILES_FILTER BIN_FILES_FILTER HEX_FILES_FILTER




namespace Ui {
    class ModelEdit;
}

class ModelEdit : public QDialog
{
    Q_OBJECT

public:
    explicit ModelEdit(EEPROMData *md, QWidget *parent = 0);
    ~ModelEdit();

    void applyBaseTemplate();
    bool redrawCurve;

    QString getNote(int i) { return mixNotes[i]; }
    void setNote(int i, QString s);
    int getModelID() { return id_model; }
    void refreshMixerList() { tabMixes(); }



    void newFile();
    bool loadFile(const QString &fileName, bool resetCurrentFile=true);
    bool save();
    bool saveAs();
    bool saveFile(const QString &fileName, bool setCurrent=true);
    QString userFriendlyCurrentFile();
    QString currentFile() { return curFile; }
    static int getFileType(const QString &fullFileName);
    bool saveToFileEnabled();

    int eepromVersion();
    QString ownerName();

private:
    Ui::ModelEdit *ui;
    EEPROMData eeData;
//    class simulatorDialog *sdptr ;

    bool fileModified;


    bool maybeSave();
    void setCurrentFile(const QString &fileName);
    void doPaste(QByteArray *gmData, int index);
    void doCopy(QByteArray *gmData);
    QString strippedName(const QString &fullFileName);

    QString curFile;
    bool isUntitled;

    void saveModelToXML(QDomDocument * qdoc, QDomElement * pe);
    void getNotesFromXML(QDomDocument * qdoc);


    MixersList *MixerlistWidget;

    QString mixNotes[MAX_MIXERS];

    EEPROMData g_model;
    int       id_model;

    bool switchEditLock;
    bool heliEditLock;
    bool protocolEditLock;
    bool plot_curve[16];

    QSpinBox  * kpidKp[MAX_KPID];
    QSpinBox  * kpidKi[MAX_KPID];
    QSpinBox  * kpidKd[MAX_KPID];
    
    QSpinBox  * cswitchOffset[NUM_SWITCHES];
    QComboBox * cswitchSource1[NUM_SWITCHES];
    QComboBox * cswitchSource2[NUM_SWITCHES];

    QDoubleSpinBox  * qsbLimitsMin[NUM_CHNOUT];
    QDoubleSpinBox  * qsbLimitsMax[NUM_CHNOUT];
    QDoubleSpinBox  * qsbLimitsPeriod[NUM_CHNOUT];
    QCheckBox * qsbLimitsInv[NUM_CHNOUT];

    QSpinBox  * safetySwitchValue[NUM_CHNOUT];
    QComboBox * safetySwitchSwtch[NUM_CHNOUT];

    void setupMixerListWidget();
    void updateSettings();
    void tabKPID();
    void tabMixes();
    void tabLimits();
    void tabCurves();
    void tabSwitches();
    void tabSafetySwitches();
    void tabTemplates();
    void updateCurvesTab();
    void setSwitchWidgetVisibility(int i);
    void setLimitMinMax();
    void updateSwitchesTab();

//    void launchSimulation();
    void resizeEvent(QResizeEvent *event  = 0);

    void setProtocolBoxes();

    void drawCurve();
    int currentCurve;
    void setCurrentCurve(int curveId);

    QSpinBox *getNodeSB(int i);

    bool gm_insertMix(int idx);
    int getMixerIndex(int dch);
    void gm_deleteMix(int index);
    void gm_openMix(int index);
    int gm_moveMix(int idx, bool dir);
    void mixersDeleteList(QList<int> list);
    QList<int> createListFromSelected();
    void setSelectedByList(QList<int> list);

    void applyTemplate(uint8_t idx);
    MixData* setDest(uint8_t dch);
    void setCurve(uint8_t c, int8_t ar[]);
    void setSwitch(uint8_t idx, uint8_t func, int8_t v1, int8_t v2);

signals:
    void modelValuesChanged(ModelEdit * = 0);


private slots:
    void documentWasModified();
    void clearMixes(bool ask=true);
    void clearCurves(bool ask=true);

    void on_tabWidget_currentChanged(int index);
    void ControlCurveSignal(bool flag);

//    void on_extendedLimitsChkB_toggled(bool checked);
    void on_resetCurve_1_clicked();
    void on_resetCurve_2_clicked();
    void on_resetCurve_3_clicked();
    void on_resetCurve_4_clicked();
    void on_resetCurve_5_clicked();
    void on_resetCurve_6_clicked();
    void on_resetCurve_7_clicked();
    void on_resetCurve_8_clicked();
    void on_resetCurve_9_clicked();
    void on_resetCurve_10_clicked();
    void on_resetCurve_11_clicked();
    void on_resetCurve_12_clicked();
    void on_resetCurve_13_clicked();
    void on_resetCurve_14_clicked();
    void on_resetCurve_15_clicked();
    void on_resetCurve_16_clicked();

    void mimeDropped(int index, const QMimeData *data, Qt::DropAction action);
    void pasteMIMEData(const QMimeData * mimeData, int destIdx=1000);
//    void on_pushButton_clicked();
    void mixersDelete(bool ask=true);
    void mixersCut();
    void mixersCopy();
    void mixersPaste();
    void mixersDuplicate();
    void mixerOpen();
    void mixerAdd();
    void moveMixUp();
    void moveMixDown();

    void mixerlistWidget_customContextMenuRequested(QPoint pos);
    void mixerlistWidget_doubleClicked(QModelIndex index);
    void mixerlistWidget_KeyPress(QKeyEvent *event);

    void on_curveEdit_1_clicked();
    void on_curveEdit_2_clicked();
    void on_curveEdit_3_clicked();
    void on_curveEdit_4_clicked();
    void on_curveEdit_5_clicked();
    void on_curveEdit_6_clicked();
    void on_curveEdit_7_clicked();
    void on_curveEdit_8_clicked();
    void on_curveEdit_9_clicked();
    void on_curveEdit_10_clicked();
    void on_curveEdit_11_clicked();
    void on_curveEdit_12_clicked();
    void on_curveEdit_13_clicked();
    void on_curveEdit_14_clicked();
    void on_curveEdit_15_clicked();
    void on_curveEdit_16_clicked();

    void on_plotCB_1_toggled(bool checked);
    void on_plotCB_2_toggled(bool checked);
    void on_plotCB_3_toggled(bool checked);
    void on_plotCB_4_toggled(bool checked);
    void on_plotCB_5_toggled(bool checked);
    void on_plotCB_6_toggled(bool checked);
    void on_plotCB_7_toggled(bool checked);
    void on_plotCB_8_toggled(bool checked);
    void on_plotCB_9_toggled(bool checked);
    void on_plotCB_10_toggled(bool checked);
    void on_plotCB_11_toggled(bool checked);
    void on_plotCB_12_toggled(bool checked);
    void on_plotCB_13_toggled(bool checked);
    void on_plotCB_14_toggled(bool checked);
    void on_plotCB_15_toggled(bool checked);
    void on_plotCB_16_toggled(bool checked);

    void curvePointEdited();
    void limitEdited();
    void kpidEdited();
    void switchesEdited();
    void safetySwitchesEdited();
    void mixesEdited();

    void refreshList();

protected:
    void closeEvent(QCloseEvent *event);


public slots:
    bool loadModelFromFile(QString fn="");
    void saveModelToFile();
    void burnTo();
    void setModified(bool mod=true);
};



#endif // MODELEDIT_H

